<script lang="ts" setup>
import dayjs from '#build/dayjs.imports.mjs'
import { getSurveyInfo } from '@/api/survey'
import { useResizeObserver } from '@vueuse/core'

const route = useRoute()
const activeNav = computed(() => route.path || '/')
const navList = [
  {
    name: '首页',
    path: '/',
  },
  {
    name: '人物库',
    path: '/peopleHealth/ExpertList',
  },
  {
    name: '新闻中心',
    path: '/peopleHealth/topNews',
  },
  {
    name: '政策解读',
    path: '/peopleHealth/healthyShaanxi',
  },
  {
    name: '书籍展示',
    path: '/peopleHealth/peopleHealthSeries',
  },
  {
    name: '志鉴分类',
    path: '/peopleHealth/knowledge',
  },
  {
    name: '西安地情',
    path: '/peopleHealth/popularization',
  },
  {
    name: '机构信息',
    path: '/peopleHealth/about',
  },
  
]

const menuHamburgerActive = ref(false)
const navName = computed(() => route.meta?.navName)
function isActive(item: any) {
  return item.path === activeNav.value || (item.path !== '/' && activeNav.value.indexOf(item.path) === 0) || navName.value === item.name
}
const { isBigCharMode } = storeToRefs(useGlobalStore())
const store = useUserStore()
const userInfo = computed(() => store.userInfo)
function showLogin() {
  navigateTo('/user/login')
}

function toChatRobot() {
  navigateTo('http://139.196.111.15:8080/ui/chat/926302390298f20a', {
    external: true,
    open: {
      target: '_blank',
    },
  })
}

const surveyInfo = ref<any>()
function toSurvey() {
  const activity_no = '20240624105711000001'
  const url = `http://139.196.111.15:9102/health/#/questionnaire/survey/survey?formType=form&activity_no=${activity_no}&status=进行中`
  navigateTo({
    path: '/peopleHealth/survey',
    query: {
      // webUrl: url,
      title: '满意度调查',
      from: 'home',
      webUrl: encodeURIComponent(url),
    },
  })
  // navigateTo({
  //   path: '/peopleHealth/questSurvey',
  //   query: {
  //     no: activity_no,
  //   },
  // })
}

const showSurvey = ref(false)
// async function getSurveyActivity(activeNo: string) {
//   const condition = [
//     {
//       colName: 'start_time',
//       ruleType: 'le',
//       value: dayjs(new Date().getTime()).format('YYYY-MM-DD'),
//     },
//     {
//       colName: 'end_time',
//       ruleType: 'ge',
//       value: dayjs(new Date().getTime()).format('YYYY-MM-DD'),
//     },
//   ]
//   const data = await getSurveyInfo(activeNo, { condition })
//   return data
// }
// onMounted(() => {
//   getSurveyActivity('20240624105711000001').then((res) => {
//     if (Array.isArray(res) && res.length > 0) {
//       surveyInfo.value = res[0]
//       showSurvey.value = true
//     }
//   })
// })

const showRobot = ref(true)

function logout() {
  ElMessageBox.confirm('确定退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      store.logout()
    })
    .catch(() => { })
}
const links = computed(() => navList.slice(0, isBigCharMode.value ? 8 : 10))
const moreNav = computed(() => navList.slice(isBigCharMode.value ? 8 : 10))
onMounted(() => {
  const { isLogin } = useUserStore()
  if (isLogin === true) {
    store.getCollectArticleList()
  }
})

const contentRef = ref<HTMLElement | null>(null)
const parentRef = ref<HTMLElement | null>(null)
const isOverflowed = ref(false)
function checkOverflow() {
  if (contentRef.value && parentRef.value) {
    console.log(contentRef.value.scrollHeight, parentRef.value.clientHeight)
    isOverflowed.value = contentRef.value.scrollHeight > parentRef.value.clientHeight
  }
}
useResizeObserver(parentRef, (entries) => {
  checkOverflow()
})
onMounted(() => {
  checkOverflow()
})
</script>

<template>
  <div class="pos-sticky top-0 z-1000 h-60px w-full bg-#dfd6c7 px-40px pb-2px shadow-md xl:px-120px">
    <div class="pos-relative mx-a flex items-center justify-center">
      <NuxtLink to="/" class="logo-icon hidden xl:block" />
      <nav ref="parentRef" class="menu-box pos-relative flex items-center justify-between">
        <div
          ref="contentRef" class="z-2 max-h-60px w-full bg-#dfd6c7 p-x-10px"
          :class="{ 'overflow-hidden': !menuHamburgerActive }"
        >
          <div class="bg-#dfd6c7">
            <NuxtLink
              v-for="nav in navList" :key="nav.name" :to="nav.path" :external="nav.external || false"
              :target="nav.external ? '_blank' : ''" class="nav-item pos-relative mx-20px px-10px"
              :class="{ 'active': isActive(nav), 'b-b-1px b-b-solid b-b-#ccc': menuHamburgerActive }"
            >
              <span class="nav-name">
                {{ nav.name }}
              </span>
            </NuxtLink>
          </div>
        </div>
      </nav>
      <button
        v-if="isOverflowed" class="reset-btn menu-hamburger m-15px w-20px cursor-pointer"
        flex="~ col justify-center" aria-label="导航" aria-expanded="false" aria-controls="full-screen"
        :class="{ active: menuHamburgerActive }" @click="menuHamburgerActive = !menuHamburgerActive"
      >
        <span class="hamburger-1" />
        <span class="hamburger-2" />
        <span class="hamburger-3" />
      </button>
      <div min-w-100px flex-1 text-right>
        <!-- <span v-if="userInfo?.user_no" class="cursor-pointer" @click="logout">{{ userInfo?.user_disp
            || userInfo?.user_no }}</span> -->
        <NuxtLink v-if="userInfo?.user_no" to="/user/userDetail">
          <el-avatar :size="40" class="cursor-pointer" :src="getImgUrl(userInfo?.header_url)">
            <img src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png">
          </el-avatar>
          <span class="inline-block float-end  ml-10px line-height-12">{{ userInfo?.real_name || userInfo?.user_no }}</span>
        </NuxtLink>
        <button v-else-if="route.path == '/user/login'" class="btn" @click="$router.go(-1)">
          返回登录
        </button>
        <button v-else class="btn" @click="showLogin">
          登录
        </button>
      </div>
    </div>
    <Login />
  </div>
</template>

<style lang="scss" scoped>
.logo-icon {
  width: 150px;
  height: 30px;
  background: url('https://xadfz.xa.gov.cn/template/images/common.files/logo.png');
  background-repeat: round;
  background-position: inherit;
  cursor: pointer;
}

.menu-box {
  transition: all 0.3s ease;
  transform: translateY(0);

  .nav-item {
    cursor: pointer;
    border-bottom: 4px solid transparent;
    line-height: 55px;
    font-size: var(--base-font-size);
    transition: font-size 0.3s ease-in-out;
    display: inline-block;
    color: black;

    &:hover {
      // border-bottom-color: var(--color-primary);
      color: black;

      .nav-item-children {
        display: flex;
        max-height: 100px;
      }
    }

    &.active {
      font-weight: 700;
      color: black;
      border-bottom-color: rgb(234, 171, 112);
    }

    .nav-item-children {
      max-height: 0;
      overflow: hidden;
      transform: translateY(100%);
      width: 200%;
      flex-direction: column;
      border: 1px solid #eee;
      border-top-color: transparent;
      color: #000;
      transition: all 0.5s ease;

      a {
        line-height: 50px;
        padding-left: 10px;
        padding-right: 10px;

        &:hover {
          color: var(--color-primary);
          background-color: #e9f7fc;
        }
      }

      @media (max-width: 768px) {
        width: 100%;
      }
    }
  }

  @media (max-width: 768px) {
    .md\:flex,
    [md\:flex=''] {
      display: flex;
    }

    &.active {
      position: absolute;
      bottom: 0;
      display: flex;
      flex-direction: column;
      width: 100%;
      transform: translateY(calc(100% - 0px));
      height: unset;

      .nav-item {
        border: none;
        line-height: 40px;
      }
    }
  }
}

button.reset-btn {
  background: none;
  border: none;
  padding: 0;
  touch-action: manipulation;
  z-index: 1;
}

.menu-hamburger {
  --text-color: #303133;
  --transition-all: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);

  span {
    background-color: var(--text-color);
    border-radius: 10px;
    height: 2px;
    margin: 2px 0;
    transition: var(--transition-all);
    width: 100%;
  }

  transform: rotate(180deg);

  .hamburger-1 {
    width: 50%;
  }

  .hamburger-3 {
    width: 75%;
  }

  &.active {
    .hamburger-3 {
      transform-origin: bottom;
      width: 50%;
      transform: translate(2px, -10px) rotate(45deg);
    }

    .hamburger-2 {
      transform-origin: top;
      transform: rotate(-45deg);
    }

    .hamburger-1 {
      transform-origin: bottom;
      transform: rotate(45deg) translate(13px);
    }
  }
}

.float-button,
.float-button2 {
  position: fixed;
  right: 20px;
  bottom: 100px;
  z-index: 99999;
  background-size: 100%;
  background-image: url('/img/doctor.png');
  /*        width: 196px;
            height: 136px; */
  width: 98px;
  height: 68px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.float-button2 {
  background-image: url('/img/wjdc.png');
  bottom: 200px;
}

.close-button {
  display: none;
  position: absolute;
  top: -20px;
  right: -20px;
  font-size: 24px;
  cursor: pointer;
  width: 30px;
  height: 30px;
  line-height: 30px;
  z-index: 99999999;
  text-align: center;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.2);
  /* font-weight: bold; */
}

.float-button:hover .close-button,
.float-button2:hover .close-button {
  display: unset;
}

.float-button2 .close-button {
  top: -10px;
  right: -10px;
}

@media screen and (min-width: 600px) {
  .float-button,
  .float-button2 {
    width: 196px;
    height: 136px;
    right: 60px;
    bottom: 300px;
  }

  .float-button2 {
    bottom: 100px;
    height: 180px;
    background-repeat: no-repeat;
  }
}
</style>
